<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\api\model\Slideshow;
use app\api\model\SpeciallyClassify;
use app\api\model\ExamVenue;
use think\Config;
use app\api\model\PracticeRecord;
use app\api\model\UserChapter;
use app\api\model\UserAnswer;
/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {

        $slideshow = Slideshow::select(); //轮播图
        if ($slideshow) {
            foreach ($slideshow as $key => $val) {
                if ($val['img']) {
                    $val['img'] = 'https://' . $_SERVER['HTTP_HOST'] . $val['img'];
                }
            }
        }
        $specially_classify = SpeciallyClassify::select();  //专场分类
        $userId = 1;$this->auth->id;
        $meiri = '今日学习待完成';
        $jingxuan = '精选练习待完成';
        $jingsai = '竞赛考试待完成';
        if ($userId) {
            $time = strtotime(date("Y-m-d"));
            $tomorrow = strtotime(date("Y-m-d",strtotime("+1 day")));
            $c = UserChapter::where(['uid'=>$userId])->whereTime('createtime', 'between', [$time, $tomorrow])->count();
            if ($c) {
                $meiri = '今日学习共'.$c.'章节';
            }
            $p = PracticeRecord::where(['uid'=>$userId])->whereTime('createtime', 'between', [$time, $tomorrow])->count();
            if ($p) {
                $jingxuan = '精选练习共'.$p.'题';
            }
            
            $a = UserAnswer::where(['uid'=>$userId])->whereTime('createtime', 'between', [$time, $tomorrow])->count();
            if ($a) {
                $jingsai = '竞赛考试共'.$a.'场';
            }
        }
        if ($specially_classify) {
            foreach ($specially_classify as $key => $val) {
                if ($val['img']) {
                    $val['img'] = 'https://' . $_SERVER['HTTP_HOST'] . $val['img'];
                }
                switch ($val['id']) {
                    case 1:
                        $specially_classify[$key]['meiri'] = $meiri;
                        break;
                    case 2:
                        $specially_classify[$key]['jingxuan'] = $jingxuan;
                        break;
                    case 3:
                        $specially_classify[$key]['jingsai'] = $jingsai;
                        break;
                }
            }
        }
        $exam_venue = ExamVenue::field('id,img,title,sign_start,sign_end')->limit(2)->select();  //专场
        if ($exam_venue) {
            foreach ($exam_venue as $key => $val) {
                if ($val['img']) {
                    $val['img'] = 'https://' . $_SERVER['HTTP_HOST'] . $val['img'];
                }
                $exam_venue[$key]['sign_end'] = $val['sign_end'] * 1000;
            }
        }
        
        $data = [
            'slideshow' => $slideshow,
            'classify'  => $specially_classify,
            'exam_venue'=> $exam_venue
        ];
        $this->success('请求成功',$data);
    }
    

    /**
     * 获取协议、声明
     */
    public function agreement() {
        $keyword = $this->request->get('keyword');
        switch ($keyword) {
            case 'statement':    //答题注意事项和免责声明
                $value = config('site.statement');
                break;
            case 'study':      //学习积分规则
                $value = config('site.study');
                break;
            case 'exercise':    //练习积分规则
                $value = config('site.exercise');
                break;
            case 'competition':      //竞赛积分规则 积分兑换规则
                $value = config('site.competition');
                break;
            case 'conversion':      // 积分兑换规则
                $value = config('site.conversion');
                break;
            case 'home':      //用户协议
                $value = config('site.home');
                break;
            case 'privacy':      // 隐私政策
                $value = config('site.privacy');
                break;
        }
        $this->success('请求成功',['value'=>$value]);
    }


}
